package com.example.projecth.projecthealth;

import com.example.projecth.R;

import android.app.Fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class CalorieFragment extends Fragment implements OnClickListener {

	private ImageButton[] menu = new ImageButton[23];
//	private Button[] menu = new Button[23];
	private TextView title;
	private EditText window;
	private int sum;
	private Boolean on = false;
	private Boolean clean = false;
	private int count=0;
	
	private MyDbHelper helper;
	private SimpleCursorAdapter adapter;
	private Cursor cursor;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.calorie_calculator, container, false);

		menu[0] = (ImageButton) view.findViewById(R.id.menu1);
		menu[1] = (ImageButton) view.findViewById(R.id.menu2);
		menu[2] = (ImageButton) view.findViewById(R.id.menu3);
		menu[3] = (ImageButton) view.findViewById(R.id.menu4);
		menu[4] = (ImageButton) view.findViewById(R.id.menu5);
		menu[5] = (ImageButton) view.findViewById(R.id.menu6);
		menu[6] = (ImageButton) view.findViewById(R.id.menu7);
		menu[7] = (ImageButton) view.findViewById(R.id.menu8);
		menu[8] = (ImageButton) view.findViewById(R.id.menu9);
		menu[9] = (ImageButton) view.findViewById(R.id.menu10);
		menu[10] = (ImageButton) view.findViewById(R.id.menu11);
		menu[11] = (ImageButton) view.findViewById(R.id.menu12);
		menu[12] = (ImageButton) view.findViewById(R.id.menu13);
		menu[13] = (ImageButton) view.findViewById(R.id.menu14);
		menu[14] = (ImageButton) view.findViewById(R.id.menu15);
		menu[15] = (ImageButton) view.findViewById(R.id.menu16);
		menu[16] = (ImageButton) view.findViewById(R.id.menu17);
		menu[17] = (ImageButton) view.findViewById(R.id.menu18);
		menu[18] = (ImageButton) view.findViewById(R.id.menu19);
		menu[19] = (ImageButton) view.findViewById(R.id.cancle);
		menu[20] = (ImageButton) view.findViewById(R.id.save);
		menu[21] = (ImageButton) view.findViewById(R.id.Clean);
		menu[22] = (ImageButton) view.findViewById(R.id.total);

		window = (EditText) view.findViewById(R.id.window);

		for (int i = 0; i < 23; i++) {
			menu[i].setOnClickListener(this);
		}
		
		helper = new MyDbHelper(getActivity(), "mydb.sqlite", null, 1);
		adapter = new SimpleCursorAdapter(getActivity(),
				android.R.layout.simple_expandable_list_item_1,
				cursor, new String[] {"col1"},
				new int[] {android.R.id.text1}, 0);

		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = null;
		ContentValues row = null;
		
		String[] items={"삼겹살", "돼지갈비", "쇠고기 등심",
						"안심 스테이크", "돼지곱창구이", "족발",
						"후라이드 치킨", "광어", "복어",
						"참치", "아귀찜", "해물탕",
						"탕수육", "짬뽕", "짜장면",
						"소주", "맥주", "레드와인", "위스키"};
		count++;
		switch (v.getId()) {
		case R.id.menu1://삼겹살
//			window.setText(String.format("%s %d +", items[0], count));
			window.setText("331");
//			window.append("삼겸살");
//			window.setText(window.getText().toString() + 331);
			sum = sum + Integer.valueOf(window.getText().toString());
//			sum = sum + Integer.parseInt(String.format("%d", R.id.menu1));
			break;
		case R.id.menu2://돼지갈비
			window.setText("208");
//			window.append("208");
//			window.setText(window.getText().toString() + 208);
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu3://쇠고기 등심
			window.setText("218");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu4://안심 스테이크
			window.setText("897");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu5://돼지곱창구이
			window.setText("737");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu6://족발
			window.setText("768");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu7://후라이드 치킨
			window.setText("269");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu8://광어
			window.setText("103");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu9://복어
			window.setText("89");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu10://참치
			window.setText("132");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu11://아귀찜
			window.setText("506");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu12://해물탕
			window.setText("289");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu13://탕수육
			window.setText("481");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu14://짬뽕 1인분
			window.setText("788");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu15://짜장면 1인분
			window.setText("864");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu16://소주 2잔(100g)
			window.setText("141");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu17://맥주 500cc
			window.setText("190");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu18://레드와인 반잔(100g)
			window.setText("70");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		case R.id.menu19://위스키(100g)
			window.setText("277");
			sum = sum + Integer.valueOf(window.getText().toString());
			break;
		
		case R.id.cancle:
			if (clean == true) {
				window.setText("");
//				clean = true;
			} else if (!window.getText().toString().equals("")) {
				sum = sum - Integer.valueOf(window.getText().toString().trim());
				window.setText(window.getText().toString().
						substring(0, window.getText().toString().length()
						- window.getText().toString().length()));
				if (on == true)
					on = false;
			}
			break;
			
		case R.id.save ://DB저장
			db = helper.getWritableDatabase();
			row = new ContentValues();
			//row.put("_id", "null");
			row.put("col1", window.toString());
		
			db.insert("memo", null, row);
			db.close();
			refreshList();
			Toast.makeText(getActivity(), "등록 완료!", Toast.LENGTH_SHORT).show();
			break;
			
		case R.id.Clean:
			sum = 0;
			window.setText("");
			break;
	
		case R.id.total:
			if (window.getText().toString().equals("")) {
				window.setText("");
			} else {
			sum = sum;
			window.setText(Integer.toString(sum));
			break;
			}
		}
	}
	private void refreshList() {

		SQLiteDatabase db = helper.getReadableDatabase();
		cursor = db.rawQuery("SELECT * FROM memo", null);
		adapter.changeCursor(cursor);
		db.close();

	}

}
